package com.iwinding.ladygg.bluetooth.impblekit;

import android.os.Handler;
import android.os.Message;

import com.iwinding.ladygg.bluetooth.ibluetooth.IHandler;
import com.iwinding.ladygg.bluetooth.impBle.BleManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 82218 on 2017/7/13.
 */

public class BleKitHandler extends Handler {

    private List<IHandler> noticeHandlers = null;

    public BleKitHandler() {
        this.noticeHandlers = new ArrayList<IHandler>();
    }

    @Override
    public void handleMessage(Message msg) {
        if (noticeHandlers.size() != 0) {
            for (IHandler noticeHandler : noticeHandlers) {
                noticeHandler.handleMessage(msg);
            }
        }
        switch (msg.what) {
            case BleManager.SERVICE_BIND:
                BleKitManager.getInstance().openBle();
                break;
            case BleManager.SERVICE_OPEN:
                BleKitManager.getInstance().scanBle();
                break;
            case BleManager.SCAN_FINSH:
                BleKitManager.getInstance().autoConnetBle();
                break;
            case BleManager.CONNET_NEXT:
                BleKitManager.getInstance().autoConnetBle();
                break;
            case BleManager.CLOSE_BLE:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(7500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        BleKitManager.getInstance().reset();
                    }
                }).start();
                break;
            case BleManager.CONNECTING:
                break;
            case BleManager.DISCONNECTED:
                break;
            case BleManager.SERVICE_SHOW:
                BleKitManager.getInstance().readRssi();
                break;
            case BleManager.PAIRING_REQUEST:
                BleKitManager.getInstance().setPin("123456");
                break;
            case BleManager.ACTION_FOUND:
//                BleDevice bleDevice = (BleDevice)msg.obj;
                break;
        }
    }

    public void register(IHandler i) {
        if (!noticeHandlers.contains(i))
            noticeHandlers.add(i);
    }

    public void unRegister(IHandler i) {
        if (noticeHandlers.contains(i))
            noticeHandlers.remove(i);
    }

}
